package register.Controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mysql.cj.util.StringUtils;

import register.Model.dao.RegisterDao;
import register.Model.dao.RegisterDaoImpl;
import register.Model.entity.Students;
import register.utils.EmailUtils;
import register.utils.JsonUntil;

@WebServlet("/register")
public class RegisterController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //转化成String类
        String jsonString=String.valueOf(JsonUntil.getJson(request));
        //转换成JSONObject类
        JSONObject jsonObject= JSON.parseObject(jsonString);
        response.setContentType("application/json;charset=utf-8");
        HttpSession session = request.getSession();
        String scode = (String) session.getAttribute("code");
        System.out.println(scode);
        System.out.println("在这");
        //获取验证码
        String code = jsonObject.getString("code");

        // 返回json数据
        JSONObject res = new JSONObject();
        if (!scode.equalsIgnoreCase(code)) {
            this.returnMsg(response, 0, "验证码不正确");
            return;
        }


        String name = jsonObject.getString("name");
        if (StringUtils.isNullOrEmpty(name)) {
            this.returnMsg(response, 0, "name不能为空");
            return;
        }
        String direction = jsonObject.getString("direction");
        if (StringUtils.isNullOrEmpty(direction)) {
            this.returnMsg(response, 0, "direction不能为空");
            return;
        }
        String phase = jsonObject.getString("phase");
        if (StringUtils.isNullOrEmpty(phase)) {
            this.returnMsg(response, 0, "phase不能为空");
            return;
        }
        RegisterDao dao = new RegisterDaoImpl();
        //是否内部员
        Integer checkO = null;
        try {
            checkO = dao.countO(name,direction,phase);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        System.out.println(checkO);
        if (checkO<=0) {
            this.returnMsg(response, 0, "注册失败,书院无此人信息");
            return;
        }
        String username = jsonObject.getString("username");
        if (StringUtils.isNullOrEmpty(username) || username.length()<4 || username.length()>12) {
            this.returnMsg(response, 0, "用户名不正确4-12位");
            return;
        }
        String password = jsonObject.getString("password");
        if (StringUtils.isNullOrEmpty(password) || password.length()<6) {
            this.returnMsg(response, 0, "密码不低于6位");
            return;
        }
        String postbox = jsonObject.getString("postbox");

        if (StringUtils.isNullOrEmpty(postbox) || !EmailUtils.validate(postbox)) {
            this.returnMsg(response, 0, "邮箱格式不正确");
            return;
        }

        String phoneNumber = jsonObject.getString("phoneNumber");
        if (StringUtils.isNullOrEmpty(phoneNumber) || phoneNumber.length()!=11) {
            this.returnMsg(response, 0, "手机号码11位");
            return;
        }


        //用户名
        Integer checkUserName = null;
        try {
            checkUserName = dao.countUserName(username);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if (checkUserName>0) {
            this.returnMsg(response, 0, "username已存在");
            return;
        }
        Students student = new Students( username,  password,  postbox,  phoneNumber,  name,  direction,
                phase);

        try {
            dao.addStudents(student);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        res.put("code", 1);
        res.put("msg", "注册成功");
        response.getWriter().write(String.valueOf(res));
    }

    private void returnMsg(HttpServletResponse response, int code, String msg) throws IOException {
        // 返回json数据
        JSONObject res = new JSONObject();
        res.put("code", code);
        res.put("msg", msg);
        response.getWriter().write(String.valueOf(res));
    }

}
